﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;

namespace CS544_Protocol_Client
{

    /**
    * Author: Kelly Page, Brian Driscoll
    * Date Created: 2/24/2011
    * Date Updated: 3/01/2011
    * 
    * Purpose: Handles chat room operations: Maintain curent userlist and
    *          chat log from server.
    * 
    **/

    public class ChatRoom
    {
        //chat room events
        public event EventHandler MessageUpdate;
        public event EventHandler UserUpdate;
    
        
        //Constructor
        public ChatRoom(string roomName)
        {
            this.RoomName = roomName;   
            this.ChatLog = new List<string>();
            this.UserList = new List<User>();
        }

        public string RoomName { get; private set; } //holds chat room name
        //public Dictionary<int,string> UserList {get; private set;} //holds user list containing userid and username from server
        public List<string> ChatLog { get; private set; } //holds chat room messages from server     
        public List<User> UserList { get; private set; }
        

        //Update current user list
        public void updateUserList(List<User> userList)
        {
            this.UserList = userList;            
            OnUserUpdate();
        }


        //Change status
        public void changeStatus(User user)
        {
            int index = 0;
            foreach (User usr in UserList)
            {
                if (user.Username == usr.Username)
                {
                    index = UserList.IndexOf(usr);
                }
            }


            UserList.RemoveAt(index);
            UserList.Insert(index, user);

        }

        
        //Adds a new message to the current chat room 
        public void addNewMessage(string username, string newMessage)
        {

            string message = username + ": " + newMessage;
            this.ChatLog.Add(message);
            OnMessageUpdate();
        
        }

        private void OnMessageUpdate()
        {
            if (MessageUpdate != null)
                MessageUpdate(this, new EventArgs());
        }

        private void OnUserUpdate()
        {
            if (UserUpdate != null)
                UserUpdate(this, new EventArgs());
        }



    }
}
